import request from "../../common/utils/request";

const GraphicTextMixCut = {
  namespaced: true,
  state: {
    // 视频混剪参数
    GraphicTextMixCutData: {},

    // 字体列表
    fontList: [],
    // 默认的字体
    defaultFont: null,

    // 花字列表
    flowerList: [],
    // 默认花字
    defaultFlower: null,
  },
  mutations: {
    // 初始化图文剪辑数据
    INIT_GRAPHICTEXT_DATA: (state, data) => {
      state.GraphicTextMixCutData = data;
    },
    // 存放字体数据
    SET_FONT_LIST: (state, data) => {
      state.fontList = data.result;
      state.defaultFont = data.default
    },
    // 存放花字列表
    SET_FLOWER_LIST: (state, data) => {
      state.flowerList = data;
    },
    // 存放默认花字
    SET_DEFAULT_FLOWER: (state, data) => {
      state.defaultFlower = data;
    },
  },
  actions: {
    // 获取字体列表
    async getFontList({ commit }, payload) { 
      return new Promise(async (resolve, reject) => {
        try {
          const res = await request.get("/work_ai", "/user/shotstack/material/fonts/list")
          console.log(res, '字体列表');
          commit('SET_FONT_LIST', res)
          // const fontPromises = [];
          res.result.forEach((font) => {
            const style = document.createElement("style");
            style.innerHTML = `
              @font-face {
                font-family: '${font.font_family}';
                src: url('${font.material_url}') format('truetype');
              }
        `;
            document.head.appendChild(style);
            // const fontFace = new FontFace(font.font_family, `url('${font.material_url}') format('truetype')`);
            // fontPromises.push(fontFace.load());
            // document.fonts.add(fontFace);
          });
            // 等待所有字体加载完成
      // await Promise.all(fontPromises);
          resolve(res)          
        } catch (error) {
          reject(error)
        }
      });
    },
        // 获取花字数据
        getFlowerListData: async ({ commit }) => {
          // const res = await request.get("/work_ai", "/user/shotstack/material/flower/list")
          return new Promise(async (resolve, reject) => {
            try {
              const presetStyles = await request.get(
                "/work_ai",
                "/user/shotstack/material/presetStyles/list"
              );
              console.log(presetStyles,'presetStylespresetStyles花字数据');
              
              // 存放花字列表
              commit("SET_FLOWER_LIST", presetStyles?.result);
              // 存放默认的系统花字
              commit("SET_DEFAULT_FLOWER", presetStyles?.default);
              resolve(presetStyles)
            } catch (error) {
              reject(error)
            }
          });
        },
  },
  getters: {
    // 判断是否所有视频组的视频不为空
    allVideoIsEmpty(state, payload) {
      return state.VideoMashup?.resource_data?.every((item) => {
        let filterMaterialList = item.material_list.filter((ele) => {
          return ele.material_url != "";
        });
        return filterMaterialList.length !== 0;
      });
    },
  },
};

export default GraphicTextMixCut;
